import router from 'koa-router';
import configTool from '../com/configTool';

const swaggerJSDoc = require('swagger-jsdoc');

const DocRoutes = new router({prefix: '/Doc'});


let swaggerSpec: any = null;
// 通配符的写法
DocRoutes
    .all('/api', async (ctx: any) => {
        if (swaggerSpec === null) {
            ctx.set('content-type', 'application/json;charset=utf-8');
            ctx.set('sis-format', 'on');
            await configTool.init();
            const baseConfig = configTool.baseConfig;
            const swaggerDefinition = {
                openapi: '3.0.0',
                info: {
                    // 自定义文档信息
                    title: '无人飞行器数据服务平台',
                    version: configTool.baseConfig.version_time,
                    description: '无人飞行器数据服务平台 接口文档'
                },
                host: `${baseConfig.localIPAddress}:${baseConfig.proxyAfterProxy_Port}`, // 指定项目地址，建议在环境参数中配置，然后用 process.env 读取
                basePath: '/'
            };

            const options = {
                failOnErrors: true,
                swaggerDefinition,
                apis: [
                    './plug/routes/*.js',
                    './src/routes/*.js'
                ] // 指定项目路由的相对路径，相对于 basePath
            };

            swaggerSpec = swaggerJSDoc(options);
        }


        ctx.body = swaggerSpec;
    });


export default DocRoutes;
